<template>
  <van-icon 
  :color="value === 1 ? '#e5645f' : '' " 
  :name="value === 1 ? 'good-job' : 'good-job-o'"
  @click="onCollect"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article' 
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async onCollect (){
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '操作中...',
        forbidClick: true // 是否禁止背景点击
      })
      try {
        // 一开始变成未点赞
        let status = -1
        // 点赞了
        if (this.value === 1){
          // 已点赞,取消点赞
          await deleteLike(this.articleId)
        } else {
          // 未点赞,可以点赞
          await addLike(this.articleId)
          status = 1
        }
        // 更新视图
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (error) {
        this.$toast.fail('操作失败,请重试!')
      }
    }
  }
}
</script>

<style>

</style>
